

#######################################################################
# Replication material for "The Social Dynamics of Collective Action: #
# Evidence from the Diffusion of the Swing Riots, 1830-31"            #
#                                                                     #
# Aidt, Leon and Satchell (2020)                                      #
#######################################################################


############################
## Table 1, column 8: MLE ##
############################

rm(list=ls())

library(spdep)
library(foreign)
library(sphet)
library(plm)
library(glmmML)
library(splm)
library(lmtest)


# Create the weights by using the cross-sectional data
riots <- read.dta("Swing_data_MLE.dta")
loc<-cbind(riots$longitude,riots$latitude)


# Create the distance weights. Goal is to create "listofweights"
nearby<-knearneigh(loc,k=10,longlat=TRUE)
lofw<-knn2nb(nearby)
matweights <- nb2mat(lofw,glist=NULL,style="B",zero.policy=NULL)
listofweights <- mat2listw(matweights)
print(listofweights, zero.policy=TRUE)

rm(matweights)
rm(loc)
rm(nearby)
rm(riots)
gc()


# Load up the data for the analysis
riots.panel <- read.dta("Swing_data_R.dta")

# The equation we are running
fm1 <- totriots ~ L1riots_near + L1totriots

# Maximum likelihood - includes individual and week effects. Adjusts for spatial errors. No external instruments.
# Put lag=FALSE because the automatically generated lags are contemporaneous, and we want them time lagged.
ml_est1 <- spml(formula=fm1, data=riots.panel, index=NULL, listw=listofweights,
                lag=FALSE, spatial.error="kkp", model="within", effect="twoways",
                method="eigen", na.action=na.fail, quiet=TRUE, zero.policy=NULL, tol.solve=1e-8,
                control=list(), legacy=FALSE)
summary(ml_est1)

